package zxing.activity;

import android.content.Intent;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.util.DisplayMetrics;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.google.zxing.common.StringUtils;

import org.litepal.crud.DataSupport;

import java.sql.Date;
import java.text.SimpleDateFormat;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnTextChanged;
import io.virtualapp.R;
import io.virtualapp.abs.ui.VActivity;
import io.virtualapp.bean.MakeBean;
import io.virtualapp.bean.NumBean;
import zxing.BitmapUtil;
import zxing.MainActivity;

public class TextCodeActivity extends VActivity {
    ImageView head_back;
    TextView head_center_title;
    @BindView(R.id.id_editor_detail_font_count)
    TextView idEditorDetailFontCount;
    @BindView(R.id.id_editor_detail)
    EditText id_editor_detail;
    @BindView(R.id.result_img)
    ImageView result_img;
    @BindView(R.id.button_text)
    Button button_text;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_text_code);
        ButterKnife.bind(this);
        getView();
    }

    public void getView() {

        head_center_title = findViewById(R.id.head_center_title);
        head_back = findViewById(R.id.head_back);
        head_center_title.setText("文本二维码");
        button_text.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (TextUtils.isEmpty(id_editor_detail.getText().toString())) {
                    Toast.makeText(TextCodeActivity.this, "文本不能为空", Toast.LENGTH_SHORT).show();
                    return;
                }
                SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy年MM月dd日 HH:mm:ss");// HH:mm:ss
                Date date = new Date(System.currentTimeMillis());
                String n = simpleDateFormat.format(date);
                MakeBean user = new MakeBean();
                user.setName(n);
                user.setTit(id_editor_detail.getText().toString() + "");
                user.save();
                NumBean numBean = new NumBean();
                if (DataSupport.findFirst(NumBean.class) != null) {
                    DataSupport.deleteAll(NumBean.class);
                }
                numBean.setNum(2);
                numBean.save();
                Intent intent = new Intent(getActivity(), CodeDisplayActivity.class);
                intent.putExtra("data", id_editor_detail.getText().toString());
                intent.putExtra("title", "文字二维码");
                startActivity(intent);
            }
        });
        head_back.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
        id_editor_detail.addTextChangedListener(new TextWatcher() {

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
            }

            @Override
            public void beforeTextChanged(CharSequence s, int start, int count,
                                          int after) {
            }

            @Override
            public void afterTextChanged(Editable s) {
                idEditorDetailFontCount.setText(String.valueOf(s.length()) + "/400");
                if (s.length() >= 400) {
                    Toast.makeText(TextCodeActivity.this, "字数上限了，亲", Toast.LENGTH_SHORT).show();
                }
            }
        });


    }


}
